cap prog drop pkgzip
program define pkgzip
  
	syntax, from(string) [replace force]

	tokenize `"`0'"', p(",")
	//local rnew=subinstr(`"`2'"',`"from(`from')"',"",1)
	local rnew=regexr(`"`2'"',"(from)(\()(.+)(\))","")
	//splitfrom `0'

	if regexm("`from'","(\.pkg)$"){
	   local from=subinstr("`from'","\","/",.)
       local     path=regexr("`from'","/[^/]+$","")
       local     pkg=regexr("`from'","^(.+)\/","")  
       local     pkg=regexr("`pkg'","(\.pkg)$","")
       net install `pkg', from(`path') `rnew' 
       exit
	}



	local opwd=c(pwd)
	local  d=c(current_date) 
	local d=subinstr("`d'"," ","",.)
	local t=c(current_time)
	local t=subinstr("`t'",":","_",.)

	mkdir _tempfile_`d'_`t'

	qui cd `opwd'\_tempfile_`d'_`t'

	local pwd=c(pwd)

	qui unzipfile `from',replace


	dirlist, fromdir(`pwd') fname(pathpkg) pattern(*.pkg)

	mata: installpkg(pathpkg,"`rnew'")

	qui cd `opwd'

	shell rmdir "`opwd'\_tempfile_`d'_`t'" /s /q
	*(https://www.statalist.org/forums/forum/general-stata-discussion/general/1310900-deleting-a-folder)

end




 
 cap mata mata function installpkg()
 
 mata:
 void function installpkg(string vector pathpkg,string scalar rnew)
 
 {
    paths=regexr(pathpkg,"/[^/]+$","")
    pkgs=regexr(pathpkg,"^(.+)\/","")  
    pkgs=regexr(pkgs,"(\.pkg)$","")
	
	for(i=1;i<=length(pathpkg);i++){
	    statainst="net install  " + pkgs[i] +",from("+paths[i]+")"+" "+rnew	
    	stata(statainst)
	}
	 
 }
 
 end
 

cap program drop splitfrom
program define splitfrom,rclass

	syntax, from(string) *

	return local from `from'
	return local renew `options'

end




cap program drop dirlist
program define dirlist

  syntax, fromdir(string) fname(string) [pattern(string) APPEND ]

  // get files in "`fromdir'" using pattern
  if "`pattern'" == "" local pattern "*"
  
  local flist: dir "`fromdir'" files "`pattern'"
  if "`append'"=="" mata: `fname'=J(0,1,"")
  
  foreach f of local flist {
      mata: `fname' =  `fname '\ "`fromdir'/`f'" 
    }
 
   local dlist: dir "`fromdir'" dirs "*"
   foreach d of local dlist{
       dirlist, fromdir("`fromdir'/`d'")  pattern("`pattern'") append fname(`fname')
   }

 end
 
 
